package com.cat.dynamicPlanning02;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-cooldown/
 * @create 2025/10/18 16:18
 * @since JDK17
 */

public class Solution24 {
    public int maxProfit(int[] prices) {
        int n = prices.length;
        if (n < 2) {
            return 0;
        }
        int[] done = new int[n], prepare = new int[n];
        done[1] = Math.max(0, prices[1] - prices[0]);
        prepare[1] = Math.max(-prices[0], -prices[1]);
        for (int i = 2; i < n; i++) {
            done[i] = Math.max(done[i - 1], prepare[i - 1] + prices[i]);
            prepare[i] = Math.max(prepare[i - 1], done[i - 2] - prices[i]);
        }

        return done[n - 1];
    }
}
